package main

import (
    "LogAgent/conf"
    "LogAgent/etcd"
    "LogAgent/kafka"
    "LogAgent/taillog"
    "fmt"
    "gopkg.in/ini.v1"
    "sync"
    "time"
)
var(
    cfg = new(conf.AppConf)
    wg sync.WaitGroup
)


func main() {
    wg.Add(1)
    //读取配置文件
    err:=ini.MapTo(cfg,"conf/config.ini")
    if err != nil {
       fmt.Printf("load conf failed, err:%v\n", err)
       return
    }

    //初始化etcd
    err = etcd.Init([]string{cfg.EtcdConf.Address}, time.Duration(cfg.EtcdConf.Timeout)*time.Second)
    if err != nil {
        fmt.Printf("init etcd failed, err:%v\n", err)
        return
    }
    fmt.Println("init etcd success.")


    //初始化kafka
    err = kafka.InitKaka([]string{cfg.KafkaConf.Address},cfg.KafkaConf.Chan_max_size)
    if err != nil {
      fmt.Printf("init kafka failed ,err:%v\n", err)
      return
    }
    fmt.Println("init kafka success")

    //从etcd中获取日志收集项的配置信息
    logEntrys, err := etcd.GetLogEntryConf(cfg.Key)
    if err != nil {
        fmt.Printf("get conf from etcd failed,err:%v\n", err)
        return
    }
    fmt.Printf("get conf from etcd success, %v\n", logEntrys)

    go taillog.WatchConfig(cfg.Key)
    taillog.Init(logEntrys);

    wg.Wait()
}
